package be.groept.ace.enquete.utils;

import java.util.Map;

import org.apache.log4j.Logger;

import be.groept.ace.enquete.model.beans.Question;
import be.groept.ace.enquete.model.beans.Survey;
import be.groept.ace.enquete.model.beans.User;
import be.groept.ace.enquete.model.enums.Language;

public class BeanUtils {
	public static void mapSurveyPostParameters(Survey survey, Map<String, String[]> parameterMap, Logger logger) {
		logger.debug("parameters: title:" + parameterMap.get("title")[0] + " | description:" + parameterMap.get("description")[0] + " | language:" + parameterMap.get("language")[0] + " | from:" + parameterMap.get("from")[0] + " | to:" + parameterMap.get("to")[0]);
		survey.setTitle(parameterMap.get("title")[0]);
		survey.setDescription(parameterMap.get("description")[0]);
		String lang = parameterMap.get("language")[0];
		if("en".equals(lang)) 
			survey.setLanguage(Language.EN);
		else 
			survey.setLanguage(Language.NL); //default
		survey.setStartdateString(parameterMap.get("from")[0]);
		survey.setEnddateString(parameterMap.get("to")[0]);
	}
	
	public static void mapQuestionPostParameters(Question question, Map<String, String[]> parameterMap, Logger logger) {
		logger.debug("parameters: question:" + parameterMap.get("question")[0] + " | possibleAnswer:" + parameterMap.get("possibleAnswer")[0] + " | freetext:" + parameterMap.get("freetext")[0]);		
		question.setText(parameterMap.get("question")[0]);
		question.setPossibleAnswer(parameterMap.get("possibleAnswer")[0]);
		question.setFreeText(parameterMap.get("freetext") == null ? false : true );
	}
	
	public static void mapUserPostParameters(User user, Map<String, String[]> parameterMap, Logger logger) {
		String[] emailv = parameterMap.get("email");
		String email;
		if( emailv == null) {
			email = parameterMap.get("originalEmail")[0];
		} else {
			email = parameterMap.get("email")[0];
		}
		logger.debug(String.format("parameters: firstname: %s, name: %s, address: %s, email: %s, password: %s, language: %s", parameterMap.get("firstname")[0], parameterMap.get("name")[0], parameterMap.get("address")[0], email, parameterMap.get("password")[0],parameterMap.get("language")[0] ));
		user.setFirstname(parameterMap.get("firstname")[0]);
		user.setName(parameterMap.get("name")[0]);
		user.setAddress(parameterMap.get("address")[0]);
		user.setEmail(email);
		user.setPassword(parameterMap.get("password")[0]);
		String lang = parameterMap.get("language")[0];
		if("en".equals(lang)) 
			user.setLanguage(Language.EN);
		else 
			user.setLanguage(Language.NL);
	}
}
